<?php

namespace addons\revolutionslider;

use app\base\classes\Hook;
use app\system\enums\ModuleGroupEnmu;
use app\base\helpers\ArrayHelper;
use addons\revolutionslider\models\Revolutionslider;
use yii\base\BootstrapInterface;

class Module extends \app\module\BaseModule implements BootstrapInterface {

    public function bootstrap($app) {
        Hook::on(\app\system\Module::EVENT_SYSTEM_GROUPNAV, [
            $this,
            "onSetGroupNav"
        ]);

        \Yii::$app->controllerMap = ArrayHelper::merge(\Yii::$app->controllerMap, [
            "revolutionslider" => 'addons\revolutionslider\controllers\RevolutionsliderController'
        ]);
    }

    public function onSetGroupNav($event) {
        $event->parameters->set(ModuleGroupEnmu::MODULE, [
            [
                'label' => "幻灯片",
                'icon' => "fa-cog",
                'url' => [
                    "/revolutionslider/index"
                ]
            ]
        ]);
    }

    public function install() {
        $sql = "CREATE TABLE " . Revolutionslider::tableName() . "(
	`revolutionslider_id` INT(11) NOT NULL AUTO_INCREMENT,
	`captions` TEXT NULL,
	`url` TEXT NULL,
	`weight` TINYINT(4) NULL DEFAULT NULL,
	PRIMARY KEY (`revolutionslider_id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB";

        $command = \Yii::$app->getDb()->createCommand($sql);
        $command->execute();
        return true;
    }

    public function uninstall() {
        $models = Revolutionslider::find()->all();

        foreach ($models as $model) {
            $model->delete();
        }

        \Yii::$app->getDb()
            ->createCommand()
            ->dropTable(Revolutionslider::tableName())
            ->execute();

        return true;
    }

    public function upgrade() {
        return true;
    }
}